<template>
  <div class="seller-layout">
    <!-- 顶部导航栏 -->
    <div class="seller-header">
      <div class="header-left">
        <div class="logo">
          <el-icon><Shop /></el-icon>
          <span>卖家中心</span>
        </div>
      </div>
      <div class="header-right">
        <el-button @click="goToMallHome" type="primary" plain>
          <el-icon><House /></el-icon>
          返回商城首页
        </el-button>
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-icon><User /></el-icon>
            {{ userStore.userName }}
            <el-icon><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主体内容 -->
    <div class="seller-main">
      <!-- 侧边栏 -->
      <div class="seller-sidebar">
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          @select="handleMenuSelect"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="dashboard">
            <el-icon><DataBoard /></el-icon>
            <span>数据概览</span>
          </el-menu-item>
          
          <el-menu-item index="products">
            <el-icon><Goods /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          
          <el-menu-item index="orders">
            <el-icon><List /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          
          <el-menu-item @click="logout" style="color: #f56c6c;">退出登录</el-menu-item>
        </el-menu>
      </div>

      <!-- 主内容区 -->
      <div class="seller-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { ElMessage } from 'element-plus'
import {
  Shop,
  User,
  ArrowDown,
  DataBoard,
  Goods,
  List,
  Money,
  Setting,
  House
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 当前激活的菜单
const activeMenu = computed(() => {
  const path = route.path
  if (path.startsWith('/seller/dashboard')) return 'dashboard'
  if (path.startsWith('/seller/products')) return 'products'
  if (path.startsWith('/seller/orders')) return 'orders'
  if (path.startsWith('/seller/finance')) return 'finance'
  if (path.startsWith('/seller/settings')) return 'settings'
  return 'dashboard'
})

// 菜单选择处理
const handleMenuSelect = (index) => {
  router.push(`/seller/${index}`)
}

// 返回商城首页
const goToMallHome = () => {
  router.push('/')
}

// 用户操作处理
const handleCommand = (command) => {
  if (command === 'logout') {
    userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/')
  }
}

const logout = () => {
  localStorage.removeItem('token');
  userStore.logout();
  router.push('/login');
};
</script>

<style scoped>
.seller-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.seller-header {
  height: 60px;
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-left .logo {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  color: #409EFF;
}

.header-left .logo .el-icon {
  margin-right: 8px;
  font-size: 24px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.header-right .user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
}

.header-right .user-info .el-icon {
  margin: 0 4px;
}

.seller-main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.seller-sidebar {
  width: 200px;
  background: #304156;
  overflow-y: auto;
}

.sidebar-menu {
  border: none;
  height: 100%;
}

.seller-content {
  flex: 1;
  background: #f5f5f5;
  overflow-y: auto;
  padding: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .seller-sidebar {
    width: 180px;
  }
  
  .seller-content {
    padding: 10px;
  }
}
</style> 